package com.freecat.adapter;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;


import com.freecat.custom.SlideLayout;
import com.freecat.database.SQLFunction;
import com.freecat.entity.gpsitem;
import com.freecat.gpstest.MainActivity;
import com.freecat.gpstest.R;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import static android.graphics.Color.*;

/**
 * 入场通知 消息适配器
 * Created by dawning on 2018/04/19.
 */
public class GpsAddressItemAdater extends BaseAdapter {
    private Context mcontext;
    private List<gpsitem> list;

    gpsitem pb;
    public SlideLayout slideLayout = null;

    SQLFunction function = new SQLFunction();


    public GpsAddressItemAdater(Context context, List<gpsitem> list) {
        this.mcontext = context;
        this.list = list;

    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        TextView tv_list_item_gps_xfsbh=null;
        TextView tv_list_item_gps_address=null;
        TextView tv_list_item_gps_glat=null;
        TextView tv_list_item_gps_glon=null;
        TextView tv_list_item_gps_time=null;
        TextView tv_list_item_gps_blat=null;
        TextView tv_list_item_gps_blon=null;
        LinearLayout line_status = null;
        TextView btn_list_imem_gps_del=null;


        if (convertView == null) {// 显示第一页的时候convertView为空
            convertView = LayoutInflater.from(mcontext).inflate(R.layout.list_item_gps_item, parent, false);
            tv_list_item_gps_address = (TextView) convertView.findViewById(R.id.tv_list_item_gps_address);
            tv_list_item_gps_glat = (TextView) convertView.findViewById(R.id.tv_list_item_gps_glat);
            tv_list_item_gps_glon = (TextView) convertView.findViewById(R.id.tv_list_item_gps_glon);
            tv_list_item_gps_time = (TextView) convertView.findViewById(R.id.tv_list_item_gps_time);
            tv_list_item_gps_blat = (TextView) convertView.findViewById(R.id.tv_list_item_gps_blat);
            tv_list_item_gps_blon = (TextView) convertView.findViewById(R.id.tv_list_item_gps_blon);
            tv_list_item_gps_xfsbh= (TextView) convertView.findViewById(R.id.tv_list_item_gps_xfsbh);
            line_status = (LinearLayout) convertView.findViewById(R.id.line_status);



            pb=list.get(position);
//            System.out.println("pb  listitems");
//            System.out.println("pb"+pb.getAddress());
//            System.out.println("pb"+pb.getGlat());
//            System.out.println("pb"+pb.getGlon());
//            System.out.println("pb"+pb.getTime());
//            System.out.println("pb"+pb.getBlat());
//            System.out.println("pb getXfsbh"+pb.getXfsbh());
            tv_list_item_gps_xfsbh.setText("消防栓编号:"+pb.getXfsbh());
            tv_list_item_gps_address.setText("地址:"+pb.getAddress());
            tv_list_item_gps_glat.setText("维度:"+pb.getGlat());
            tv_list_item_gps_glon.setText("经度:"+pb.getGlon());
            tv_list_item_gps_time.setText("时间:"+pb.getTime());
            tv_list_item_gps_blat.setText(pb.getBlat());
            tv_list_item_gps_blon.setText(pb.getBlon());
//            if (pb.getStatus().equals("5")){
//
//                line_status.setBackgroundColor(BLUE);
//            }



        }

        btn_list_imem_gps_del = convertView.findViewById(R.id.btn_list_imem_gps_del);

        final gpsitem gpsitem = list.get(position);
        btn_list_imem_gps_del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SlideLayout slideLayout = (SlideLayout) v.getParent();
                slideLayout.closeMenu(); //解决删除item后下一个item变成open状态问题
//                list.remove(myContent);
                showDialog_db_delete(gpsitem);

            }
        });

        SlideLayout slideLayout = (SlideLayout) convertView;
        slideLayout.setOnStateChangeListener(new MyOnStateChangeListener());



        return convertView;
    }


    public void showDialog_db_delete(final gpsitem gpsitem1){

        AlertDialog.Builder builder = new AlertDialog.Builder(mcontext);
        builder.setTitle("删除该条数据");// 设置标题
        // builder.setIcon(R.drawable.ic_launcher);//设置图标
        builder.setMessage("确认是否删除该条数据");// 为对话框设置内容
        // 为对话框设置取消按钮
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface arg0, int arg1) {
                // TODO Auto-generated method stub

            }
        });
        // 为对话框设置中立按钮
        // builder.setNeutralButton("中立", new OnClickListener() {
        //
        // @Override
        // public void onClick(DialogInterface arg0, int arg1) {
        // // TODO Auto-generated method stub
        // myToast("您点击了中立按钮");
        // }
        // });
        // 为对话框设置确定按钮
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface arg0, int arg1) {
                // TODO Auto-generated method stub

//                Toast.makeText(mcontext, "数据库初始化成功", Toast.LENGTH_SHORT).show();

//        String sql = "delete from person where _id =  ? ";

                String sql="delete from gpsaddress where id =  ?";
                Object[] data ={gpsitem1.getId()};
                function.delete(mcontext,data,sql);

                list.remove(gpsitem1);
                notifyDataSetChanged();

            }
        });
        builder.create().show();// 使用show()方法显示对话框

    }


    private Set<SlideLayout> sets = new HashSet();

    class MyOnStateChangeListener implements SlideLayout.OnStateChangeListener
    {
        /**
         * 滑动后每次手势抬起保证只有一个item是open状态，加入sets集合中
         **/
        @Override
        public void onOpen(SlideLayout layout) {
            slideLayout = layout;
            if (sets.size() > 0) {
                for (SlideLayout s : sets) {
                    s.closeMenu();
                    sets.remove(s);
                }
            }
            sets.add(layout);
        }

        @Override
        public void onMove(SlideLayout layout) {
            if (slideLayout != null && slideLayout !=layout)
            {
                slideLayout.closeMenu();
            }
        }

        @Override
        public void onClose(SlideLayout layout) {
            if (sets.size() > 0) {
                sets.remove(layout);
            }
            if(slideLayout ==layout){
                slideLayout = null;
            }
        }
    }


}
